package org.cid54.android.jshopper;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.List;
import org.cid54.android.jshopper.dao.DaoCallBackMethod;
import org.cid54.android.jshopper.dao.ShoppingListDao;
import org.cid54.android.jshopper.dao.UserGroupDao;
import org.cid54.android.jshopper.dao.impl.ShoppingListDaoImpl;
import org.cid54.android.jshopper.dao.impl.UserGroupDaoImpl;
import org.cid54.android.jshopper.model.ShoppingList;

/**
 *
 * @author fgo
 */
public class NewShoppingList extends Activity {

    private final ShoppingListDao shoppingListDao = new ShoppingListDaoImpl();
    private final UserGroupDao userGroupDao = new UserGroupDaoImpl();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.new_shoppinglist);

        this.userGroupDao.getUserGroups(new DaoCallBackMethod<List<String>>() {

            public void execute(List<String> t) {
                doLayout(t);
            }
        });
    }

    private void doLayout(List<String> userGroups) {

        final EditText shoppingListNameTextBox = (EditText) this.findViewById(R.id.name_entry);
        final Spinner userGroupNameSpinner = (Spinner) this.findViewById(R.id.usergroup_entry);

        ArrayAdapter<String> userGroupAdapter = new ArrayAdapter<String>(this.getApplicationContext(), android.R.layout.simple_spinner_item, userGroups);

        userGroupAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        userGroupNameSpinner.setAdapter(userGroupAdapter);

        Button okButton = (Button) this.findViewById(R.id.save);

        okButton.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {

                ShoppingList shoppingList = new ShoppingList();

                shoppingList.setName(shoppingListNameTextBox.getText().toString());

                shoppingList.setUserGroupName(userGroupNameSpinner.getSelectedItem() + "");

                shoppingListDao.saveShoppingList(shoppingList, new DaoCallBackMethod<Boolean>() {

                    public void execute(Boolean t) {
                        if (t) {
                            finish();
                        }
                    }
                });

            }
        });

    }
}
